package net.hvacapp.cmvrfforandroid.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.adapter.BaseAdapterHelper;
import net.hvacapp.cmvrfforandroid.adapter.QuickAdapter;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.mine.model.GetStatisticsDataResp;
import net.hvacapp.cmvrfforandroid.ui.mine.model.StatisticsData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_statistics_list)
public class StatisticsListActivity extends BaseActivity {

    @ViewById(R.id.lv_templete)
    ListView mTempleteLv;

    private QuickAdapter<StatisticsData> mQuickAdapter;



    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        initViews();
        initEvent();
    }

    /***
     * 初始化视图
     */
    private void initViews() {

    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "数据统计", null);

        if (mQuickAdapter == null){
            mQuickAdapter = new QuickAdapter<StatisticsData>(context, R.layout.two_lines_cell_item) {
                @Override
                protected void convert(BaseAdapterHelper helper, StatisticsData item) {

                    helper.setImageResource(R.id.iv_selected_mark, R.drawable.user_selection_hint);
                    helper.setVisible(R.id.iv_selected_mark, true);
                    helper.setText(R.id.tv_title, item.getTitle());
                    helper.setText(R.id.tv_description, item.getComment());
                }
            };
        }

        mTempleteLv.setAdapter(mQuickAdapter);

    }


    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        loadStatisticsData();
    }



    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }



    private void initEvent() {

        mTempleteLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                StatisticsData data = (StatisticsData) parent.getItemAtPosition(position);

                Intent intent = new Intent(StatisticsListActivity.this, AnnotationUtil.get(StatisticsFilterActivity.class));
                Bundle bundle = new Bundle();
                bundle.putString("guidId", data.getGuidId());
                bundle.putString("title", data.getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


    }


    private void loadStatisticsData() {
        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_ALL_STATISTICS_DATA, AccountManager.getInstance().getToken(), new HttpCallbackHandle<GetStatisticsDataResp>() {
            @Override
            public void onSuccess(GetStatisticsDataResp obj) {
                // 成功
                mQuickAdapter.addAll(obj.getGetAllStatisticTemplateResult());
            }

            @Override
            public void onFailure(String response) {
                showMessage("获取模板失败，请重试！");
            }
        });
    }




}
